package com.nightwalkbuddy.dto;

import java.util.NavigableMap;
import java.util.TreeMap;

/**
 * The Path on the map is a collection of latitude and longitude. The path
 * deviation and length will be compared to track user. NavigableMap will be
 * reversed for round trip tracking. Reverse route is a planned future
 * implementation.
 * 
 * @author - htisabel
 */
public class MapPath {

	private NavigableMap<Integer, MapPair<Double, Double>> aMap = new TreeMap<Integer, MapPair<Double, Double>>();
	private int pathSection = 1;
	String mapPrint = "";
	private MapPair<Double, Double> mapPointPair;

	/**
	 * @return the mapPointPair
	 */
	public MapPair<Double, Double> getMapPointPair() {
		return mapPointPair;
	}

	/**
	 * method to pass MapPointPair to populate the MapPath
	 * 
	 * @param latitude
	 *            current user latitude
	 * @param longitude
	 *            current user longitude
	 */
	public void setMapPointPair(double latitude, double longitude) {
		mapPointPair = new MapPair<Double, Double>(latitude, longitude);
	}

	/**
	 * method to get the saved map path
	 * 
	 * @return the aMap
	 */
	public NavigableMap<Integer, MapPair<Double, Double>> getaMap() {
		return aMap;
	}

	/**
	 * method to set the map path using lat/long data from GPS service this is
	 * used for tracking user during the walk
	 * 
	 */
	public void setaMap() {
		aMap.put(getPathSection(), getMapPointPair());
		mapPrint += mapPointPair.getFirst() + " and "
				+ mapPointPair.getSecond() + ", ";
		pathSection++;
	}

	/**
	 * print MapPath from location to location
	 * 
	 * @return literal lat/long values of a location along the path
	 */
	public String printMapPath() {
		String pathInProgress = "";
		for (int key = 1; key < getPathSection(); key++) {
			pathInProgress += " " + aMap.get(key).getFirst() + " and "
					+ aMap.get(key).getSecond() + ",";
		}
		return pathInProgress;
	}

	/**
	 * method to retrieve saved mapPath from SQLite db set the current mapPrint
	 * for later reverse route implementation
	 * 
	 * @param mapPath
	 */
	public void setMapPrint(String mapPath) {
		mapPrint = mapPath;
	}

	/**
	 * method to popup Toast during development for checking the saved path
	 * later on it is used for reverse route implementation
	 * 
	 * @return String saved mapPath
	 */
	public String getMapPrint() {
		return mapPrint;
	}

	/**
	 * method to calculate the return deviation along the way
	 * 
	 * @return double point-to-point deviation
	 */
	public double getDevi() {
		double aDeviation = 0;

		double plusSecondKey = aMap.get(getPathSection() - 1).getSecond();
		double plusFirstKey = aMap.get(getPathSection() - 1).getFirst();
		double secondKey = aMap.get(getPathSection() - 2).getSecond();
		double firstKey = aMap.get(getPathSection() - 2).getFirst();
		aDeviation = Math.atan2(plusSecondKey - secondKey, plusFirstKey
				- firstKey);

		return aDeviation;
	}

	/**
	 * method to count numbers of path points recorded along the way
	 * 
	 * @return pathSection integer
	 */
	private int getPathSection() {
		return pathSection;
	}

}
